package modelo;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.MesaEntity;
import entities.MozoEntity;
import entities.SectorEntity;
import hbt.AbstractHibernateManager;

public class SectorDao extends AbstractHibernateManager {

	private static SectorDao instancia = null;
	
	public static SectorDao getInstancia()
	{
		if(instancia == null)
		{
			instancia = new SectorDao();
		}
		return instancia;
	}
	
	public void save(SectorEntity sec){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.persist(sec);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[sector PERSISTED ON TABLE sectores]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	
	public void guardar(SectorEntity sec){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.save(sec);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[sector SAVED ON TABLE sectores]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	/**
	 * Retorna los datos de un sector en particular
	 * @param idSector
	 * @return List<Object[]> con los datos del sector, mesas y mozos
	 */
	@SuppressWarnings("unchecked")
	public SectorEntity obtenerSector(Integer idSector) {
		Session s = null;
		SectorEntity sector = new SectorEntity();
		
		try {
			s = getNewSession();
			sector =  (SectorEntity) s.createQuery("FROM SectorEntity sector " +
									"WHERE sector.id = :idSector")
							  .setInteger("idSector", idSector)
							  .uniqueResult();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return sector;
	}
	
}
